//this file is part of ePopup
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#pragma once
#include "TrayDialog.h"
#include "resource.h"
#include <string>
#include "Popup.h"
#include "PopupClientUI.h"

#ifdef _DEBUG
#define POPUP_GUID				_T("POPUP-{4EADC6FC-516F-4b7c-9066-97D893649570}-DEBUG")
#else
#define POPUP_GUID				_T("POPUP-{4EADC6FC-516F-4b7c-9066-97D893649570}")
#endif

class CTaskbarNotifier;
class PopupClient;
class PopupMainDialog;

enum PopupWndMessages
{
	UM_FIRST_POPUP_USER_MSG = (WM_USER + 0x100 + 1),
	UM_TRAY_ICON_NOTIFY_MESSAGE,
	UM_REPLY_BTN_CLICKED,
	UM_REPLYALL_BTN_CLICKED,
};

class CpopupDlg : public CTrayDialog, PopupClientUI
{
public:

	CpopupDlg(CWnd* pParent = NULL);
	~CpopupDlg();

	enum { IDD = IDD_POPUP_DIALOG };
	bool m_noSleep;

	virtual void RestoreWindow();
	CTaskbarNotifier* m_wndTaskbarNotifier;
	CTaskbarNotifier* m_wndTaskbarNotifierText;
	
	virtual void EndDialog(int res);

	// PopupClientUI implementation
	void notifyMessage(const PopupMessageInfo & p_message, bool _isReplay = false);
	void updateConnectionInfos(bool p_connected, const std::string & p_tooltipinfo);
	void kill();
	void appendLog(const std::string & p_log);
	void clearClients();
	void updateClientStatus(const PopupClientInfo & p_client);
	void notifyNetworkError(const std::string & p_error);
	std::string getTemporaryDir();

	bool setMode(const PopupClientMode & p_mode);
	inline bool isMuted() const { return m_mode == POPUP_MODE_MUTED; }
	inline void resetTimer() { m_resetTimer = true; }
	void startServer();
	void recordMessageSent(const PopupMessageInfo & p_message);

	bool m_resetTimer;
	bool m_doServer;
	PopupClientMode m_mode;
	PopupMainDialog *m_mainDialog;

	// Mutex to protect access to events list
	static CMutex       s_eventmutex;
	
protected:
	
	HICON m_hIcon, m_hIconMute, m_hIconPrompt, m_hIconTextOnly;
	bool m_connected;
	bool m_senderDialogOn;
	
	CpopupApp *m_app;
	PopupMessageList m_incomingMessages;
	std::string m_connectionInfos;
	PopupMessageInfo m_currentMessageDisplayed;

	void MinimizeWindow();
	virtual BOOL OnInitDialog();
	void updateToolTip();
	void OnTrayRButtonUp(CPoint pt);
	bool isReadyToDisplayNextMessage();
	void displayMessage(const PopupMessageInfo & p_message);
	void updateIcon();
	bool acceptMessage(const PopupMessageInfo & p_message);
	virtual void OnTrayLButtonDown(CPoint pt);
	void notifyMode();
	void OnOK();

	friend class PopupEventThread;

	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnTcnSelchangeTab1(NMHDR *pNMHDR, LRESULT *pResult);
	afx_msg LRESULT OnReply(WPARAM wParam,LPARAM lParam);
	afx_msg LRESULT OnReplyAll(WPARAM wParam,LPARAM lParam);
};
